package com.seal.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.seal.comm.utils.Page;
import com.seal.dao.BaseDaoImpl;
import com.seal.dao.IOrderInfoDao;
import com.seal.dto.OrderInfoDto;
import com.seal.model.OrderInfo;
import com.seal.model.UserInfo;
@Repository("orderInfoDaoImpl")
@SuppressWarnings("unchecked")
public class OrderInfoDaoImpl extends BaseDaoImpl implements IOrderInfoDao {
	
	public OrderInfo getOrderInfoById(Long id) throws Exception {
		return (OrderInfo) this.select(OrderInfo.class, id);
	}

	public void savaOrUpdateOrderInfo(OrderInfo info) throws Exception {
		this.saveOrUpdate(info);
	}

	public void saveOrderInfo(OrderInfo info) throws Exception {
		this.save(info);
	}

	public void updateOrderInfo(OrderInfo info) throws Exception {
		this.update(info);
	}
	
	public List<OrderInfo> getOrderInfoByUserId(UserInfo userInfo)
			throws Exception {
		String hql = new StringBuffer(" from OrderInfo t where t.userName = ? order by t.id desc ").toString();
		Query query = this.getSessionFactory().getCurrentSession().createQuery(hql).setString(0, userInfo.getUserId());		
		return query.list();
	}

	public List<OrderInfo> getOrderInfoList(OrderInfoDto dto, Page page)
			throws Exception {
		StringBuffer hql = new StringBuffer(" from OrderInfo t where  1 = 1 ");
		
		if(null != dto.getCompanyId())
			hql.append(" and t.companyId = :companyId ");
		
		if(!StringUtils.isEmpty(dto.getStatus()))
			hql.append(" and t.status = :status ");
		
		if(!StringUtils.isEmpty(dto.getUserId()))
			hql.append(" and t.userId = :userId ");
		
		if(null != dto.getBeginDate())
			hql.append(" and t.createDate >= :beginDate ");
		
		if(null != dto.getEndDate())
			hql.append(" and t.createDate <= :endDate ");
		
		hql.append(" order by id desc");
		Query query = getSessionFactory().getCurrentSession().createQuery(hql.toString());
		query.setProperties(dto);
		
		if(null != page){
			query.setFirstResult(page.getFirstResult());
			query.setMaxResults(page.getPageSize());
			page.setTotalNumber(this.getOrderInfoCount(dto, page));
		}
		return query.list();
	}
	
	private int getOrderInfoCount(OrderInfoDto dto, Page page) throws Exception{
		StringBuffer hql = new StringBuffer(" select count(t) from OrderInfo t where  1 = 1 ");
		
		if(null != dto.getCompanyId())
			hql.append(" and t.companyId = :companyId ");
		
		if((null != dto.getStatus()) && !StringUtils.isEmpty(dto.getStatus()))
			hql.append(" and t.status = :status ");
		
		if((null != dto.getUserId()) && !StringUtils.isEmpty(dto.getUserId()))
			hql.append(" and t.userId = :userId ");
		
		if(null != dto.getBeginDate())
			hql.append(" and t.createDate >= :beginDate ");
		
		if(null != dto.getEndDate())
			hql.append(" and t.createDate <= :endDate ");
		
		Query query = getSessionFactory().getCurrentSession().createQuery(hql.toString());
		query.setProperties(dto);
		
		return ((Number)query.uniqueResult()).intValue();
	}

}
